//
//  JWKStatusOriginalView.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/24.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit
import SnapKit


class JWKStatusOriginalView: UIView {
    
   private var bottomCons : Constraint?
    
    //模型属性
    var statusViewModel : JWKStatusViewModel?{
        didSet{
            
            self.backgroundColor = UIColor.yellowColor()
            //关联属性和模型
            let usermodel = statusViewModel?.status?.user
            //iconimageView

            iconImageView.sd_setImageWithURL(statusViewModel?.iconUrl)
            

            
            //verifiedImage是可选项,nil时没有大V图片框
            verifiedImageView.image = statusViewModel?.verifiedImage

            
            //nameLabel
            nameLabel.text = usermodel?.name
            
            //mbrankImageView等级视图
            //拼接图片名
            mbrankImageView.image = statusViewModel?.mbrankImage
            
            //TODO:
            //created_atLabel
            created_atLabel.text = statusViewModel?.created_at
            
            //sourceLabel
            sourceLabel.text = statusViewModel?.source
            
            //contentLabel
            contentLabel.text = statusViewModel?.status?.text
            contentLabel.preferredMaxLayoutWidth = UIScreenWidth - 2*8
            
            
//            pictureView.pictures = statusViewModel?.status?.pic_urls
            
            //卸载
            bottomCons?.uninstall()
            
            if let images = statusViewModel?.status?.pic_urls where images.count > 0{
                //有配图
                pictureView.pictures = images
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(standardMargin).constraint
                })
                //显示视图
                pictureView.hidden = false
                
            }else{
                //没有配图
                
                //设置数据
                pictureView.pictures = nil
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(standardMargin).constraint
                })
                //隐藏视图
                pictureView.hidden = true
            }
            
//            //设置contentLabel的底部到view底部的约束
//            contentLabel.snp_makeConstraints { (make) in
//                make.bottom.equalTo(self).offset(-8)
//            }
            
            
        }
    }

    //懒加载子视图
    //头像视图
    
   lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //认证视图
    lazy var verifiedImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))

    
    //昵称文案
    lazy var nameLabel : UILabel = UILabel.init(title: "测试", textColor: UIColor.darkGrayColor(), textAlignment: .Center, font: 16)
    
    //vip等级视图
    lazy var mbrankImageView : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    //发布时间label
    lazy var created_atLabel : UILabel = UILabel.init(title: "20:02", textColor: UIColor.orangeColor(), textAlignment: .Left, font: 13)
    
    //来源label
    lazy var sourceLabel : UILabel = UILabel.init(title: "weibo.com", textColor: UIColor.orangeColor(), textAlignment: .Left, font: 13)
    
    //正文label
    lazy var contentLabel : UILabel = UILabel.init(title: "我是一个兵,来自老百姓,我们都有一个家,名字叫中国,我是老王,我为自己代言", textColor: UIColor.darkGrayColor(), textAlignment: .Left, font: 16)
    
    //配图
    lazy var pictureView : JWKStatusPictureView = JWKStatusPictureView()
    
    
    //构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        //设置UI
        setupUI()
    }
    
    
    func setupUI(){
        addSubview(iconImageView)
        addSubview(verifiedImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(created_atLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        iconImageView.snp_makeConstraints { (make) in
            make.left.top.equalTo(self).offset(8)
            make.width.equalTo(50)
            make.height.equalTo(50)
        }
        
        verifiedImageView.snp_makeConstraints { (make) in
            make.bottom.right.equalTo(iconImageView)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImageView)
            make.left.equalTo(iconImageView.snp_right).offset(8)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(8)
        }
        
        created_atLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(iconImageView)
            make.left.equalTo(iconImageView.snp_right).offset(8)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(created_atLabel)
            make.left.equalTo(created_atLabel.snp_right).offset(8)
        }

        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp_bottom).offset(8)
            make.left.equalTo(iconImageView)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(standardMargin)
            make.left.equalTo(contentLabel)
        }
        
        //底部约束
        self.snp_makeConstraints(closure: { (make) in
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(standardMargin).constraint
        })
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    

}
